﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfoService.Utils;

namespace InfoService.Feeds
{
    public static class FeedUpdater
    {
        public static void SetupUpdater()
        {
            UpdateTimer.OnTimeForUpdateProperties += new UpdateTimer.UpdatePropertiesHandler(UpdateTimer_UpdateProperties);
        }
        static void UpdateTimer_UpdateProperties()
        {
            if (FeedService.Enabled)
            {

                //Jump over the first Update and fake the LastRefresh to NOW
                if (!FeedService.UpdateOnStartup)
                {
                    FeedService.LastRefresh = DateTime.Now;
                    FeedService.UpdateOnStartup = true;
                }
                TimeSpan spanFeed = DateTime.Now - FeedService.LastRefresh;
                if (((int)spanFeed.TotalMinutes) >= FeedService.RefreshInterval)
                {
                    if (FeedService.Feeds.Count >= 1) FeedUtils.UpdateFeedDataAsync();
                }
            }
        }
    }
}
